package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> all = baseMapper.selectList(null);

        //找到所有的一级分类
        List<CategoryEntity> collect = all.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == 0;
        }).map(categoryEntity -> {
               categoryEntity.setChildren(getChilrenCid(categoryEntity,all));
               return categoryEntity;
        }).sorted((menu1,memu2)->{
            return  menu1.getSort() -memu2.getSort();
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void removeListByIds(List<Long> catIds) {

        //批量删除接口，先判断删除条件
        baseMapper.deleteBatchIds(catIds);
    }

    @Override
    public List<Long> findCatelogPath(Long attrGroupId) {
        List<Long> path = new ArrayList<>();
        List<Long> parentPath = findParentPath(attrGroupId, path);
        Collections.reverse(parentPath);
        return parentPath;
    }

    @Override
    public void updateDetail(Long catId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setCatelogId(catId);
        relationEntity.setCatelogName(name);
        categoryBrandRelationService.update(relationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

    private List<Long> findParentPath(Long attrGroupId, List<Long> path) {
        path.add(attrGroupId);
        CategoryEntity entity = this.getById(attrGroupId);
        if (entity.getParentCid()!=0){
            findParentPath(entity.getParentCid(),path);
        }
        return path;

    }

    //找到所有子菜单
    private List<CategoryEntity> getChilrenCid(CategoryEntity categoryEntity, List<CategoryEntity> all) {

        List<CategoryEntity> collect = all.stream().filter((item) -> {
            return item.getParentCid() == categoryEntity.getCatId();
        }).map(categoryEntity2 -> {
            categoryEntity2.setChildren(getChilrenCid(categoryEntity2,all));
            return categoryEntity2;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }

}